View Javadoc

1   // Array.java, created Thu Jul  4  4:50:03 2002 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.ClassLib.Common.java.lang.reflect;
5   
6   import joeq.Allocator.ObjectLayout;
7   import joeq.Class.jq_Array;
8   import joeq.Class.jq_Primitive;
9   import joeq.Class.jq_Reference;
10  import joeq.Class.jq_Type;
11  import joeq.ClassLib.ClassLibInterface;
12  import joeq.Memory.HeapAddress;
13  import jwutil.util.Convert;
14  
15  /***
16   * Array
17   *
18   * @author  John Whaley <jwhaley@alum.mit.edu>
19   * @version $Id: Array.java 1941 2004-09-30 03:37:06Z joewhaley $
20   */
21  public abstract class Array {
22  
23      public static int getLength(Object array) throws IllegalArgumentException {
24          if (!jq_Reference.getTypeOf(array).isArrayType())
25              throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
26          return HeapAddress.addressOf(array).offset(ObjectLayout.ARRAY_LENGTH_OFFSET).peek4();
27      }
28      public static Object get(Object array, int index)
29      throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
30          if (array == null) throw new NullPointerException();
31          if (array instanceof Object[]) return ((Object[])array)[index];
32          if (array instanceof int[]) return new Integer(((int[])array)[index]);
33          if (array instanceof long[]) return new Long(((long[])array)[index]);
34          if (array instanceof float[]) return new Float(((float[])array)[index]);
35          if (array instanceof double[]) return new Double(((double[])array)[index]);
36          if (array instanceof boolean[]) return Convert.getBoolean(((boolean[])array)[index]);
37          if (array instanceof byte[]) return new Byte(((byte[])array)[index]);
38          if (array instanceof short[]) return new Short(((short[])array)[index]);
39          if (array instanceof char[]) return new Character(((char[])array)[index]);
40          throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
41      }
42      public static boolean getBoolean(Object array, int index) {
43          if (array == null) throw new NullPointerException();
44          if (array instanceof boolean[]) return ((boolean[])array)[index];
45          throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
46      }
47      public static byte getByte(Object array, int index) {
48          if (array == null) throw new NullPointerException();
49          if (array instanceof byte[]) return ((byte[])array)[index];
50          throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
51      }
52      public static char getChar(Object array, int index) {
53          if (array == null) throw new NullPointerException();
54          if (array instanceof char[]) return ((char[])array)[index];
55          throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
56      }
57      public static short getShort(Object array, int index) {
58          if (array == null) throw new NullPointerException();
59          if (array instanceof short[]) return ((short[])array)[index];
60          throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
61      }
62      public static int getInt(Object array, int index) {
63          if (array == null) throw new NullPointerException();
64          if (array instanceof int[]) return ((int[])array)[index];
65          throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
66      }
67      public static long getLong(Object array, int index) {
68          if (array == null) throw new NullPointerException();
69          if (array instanceof long[]) return ((long[])array)[index];
70          throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
71      }
72      public static float getFloat(Object array, int index) {
73          if (array == null) throw new NullPointerException();
74          if (array instanceof float[]) return ((float[])array)[index];
75          throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
76      }
77      public static double getDouble(Object array, int index) {
78          if (array == null) throw new NullPointerException();
79          if (array instanceof double[]) return ((double[])array)[index];
80          throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
81      }
82      public static void set(Object array, int index, Object value)
83      throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
84          if (array == null) throw new NullPointerException();
85          if (array instanceof Object[]) {
86              ((Object[])array)[index] = value;
87              return;
88          }
89          if (array instanceof boolean[]) {
90              boolean v;
91              if (value instanceof Boolean) v = ((Boolean)value).booleanValue();
92              else throw new IllegalArgumentException("cannot store value of type "+jq_Reference.getTypeOf(value)+" into array of type "+jq_Reference.getTypeOf(array));
93              ((boolean[])array)[index] = v;
94              return;
95          }
96          if (array instanceof byte[]) {
97              byte v;
98              if (value instanceof Byte) v = ((Byte)value).byteValue();
99              else throw new IllegalArgumentException("cannot store value of type "+jq_Reference.getTypeOf(value)+" into array of type "+jq_Reference.getTypeOf(array));
100             ((byte[])array)[index] = v;
101             return;
102         }
103         if (array instanceof short[]) {
104             short v;
105             if (value instanceof Short) v = ((Short)value).shortValue();
106             else if (value instanceof Byte) v = (short)((Byte)value).byteValue();
107             else throw new IllegalArgumentException("cannot store value of type "+jq_Reference.getTypeOf(value)+" into array of type "+jq_Reference.getTypeOf(array));
108             ((short[])array)[index] = v;
109             return;
110         }
111         if (array instanceof char[]) {
112             char v;
113             if (value instanceof Character) v = ((Character)value).charValue();
114             else if (value instanceof Byte) v = (char)((Byte)value).byteValue();
115             else throw new IllegalArgumentException("cannot store value of type "+jq_Reference.getTypeOf(value)+" into array of type "+jq_Reference.getTypeOf(array));
116             ((char[])array)[index] = v;
117             return;
118         }
119         if (array instanceof int[]) {
120             int v;
121             if (value instanceof Integer) v = ((Integer)value).intValue();
122             else if (value instanceof Character) v = (int)((Character)value).charValue();
123             else if (value instanceof Short) v = (int)((Short)value).shortValue();
124             else if (value instanceof Byte) v = (int)((Byte)value).byteValue();
125             else throw new IllegalArgumentException("cannot store value of type "+jq_Reference.getTypeOf(value)+" into array of type "+jq_Reference.getTypeOf(array));
126             ((int[])array)[index] = v;
127             return;
128         }
129         if (array instanceof long[]) {
130             long v;
131             if (value instanceof Long) v = ((Long)value).longValue();
132             else throw new IllegalArgumentException("cannot store value of type "+jq_Reference.getTypeOf(value)+" into array of type "+jq_Reference.getTypeOf(array));
133             ((long[])array)[index] = v;
134             return;
135         }
136         if (array instanceof float[]) {
137             float v;
138             if (value instanceof Float) v = ((Float)value).floatValue();
139             else throw new IllegalArgumentException("cannot store value of type "+jq_Reference.getTypeOf(value)+" into array of type "+jq_Reference.getTypeOf(array));
140             ((float[])array)[index] = v;
141             return;
142         }
143         if (array instanceof double[]) {
144             double v;
145             if (value instanceof Double) v = ((Double)value).doubleValue();
146             else throw new IllegalArgumentException("cannot store value of type "+jq_Reference.getTypeOf(value)+" into array of type "+jq_Reference.getTypeOf(array));
147             ((double[])array)[index] = v;
148             return;
149         }
150         throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
151     }
152     public static void setBoolean(Object array, int index, boolean z)
153     throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
154         if (array == null) throw new NullPointerException();
155         if (array instanceof boolean[]) ((boolean[])array)[index] = z;
156         throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
157     }
158     public static void setByte(Object array, int index, byte z)
159     throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
160         if (array == null) throw new NullPointerException();
161         if (array instanceof byte[]) ((byte[])array)[index] = z;
162         throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
163     }
164     public static void setChar(Object array, int index, char z)
165     throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
166         if (array == null) throw new NullPointerException();
167         if (array instanceof char[]) ((char[])array)[index] = z;
168         throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
169     }
170     public static void setShort(Object array, int index, short z)
171     throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
172         if (array == null) throw new NullPointerException();
173         if (array instanceof short[]) ((short[])array)[index] = z;
174         throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
175     }
176     public static void setInt(Object array, int index, int z)
177     throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
178         if (array == null) throw new NullPointerException();
179         if (array instanceof int[]) ((int[])array)[index] = z;
180         throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
181     }
182     public static void setLong(Object array, int index, long z)
183     throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
184         if (array == null) throw new NullPointerException();
185         if (array instanceof long[]) ((long[])array)[index] = z;
186         throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
187     }
188     public static void setFloat(Object array, int index, float z)
189     throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
190         if (array == null) throw new NullPointerException();
191         if (array instanceof float[]) ((float[])array)[index] = z;
192         throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
193     }
194     public static void setDouble(Object array, int index, double z)
195     throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
196         if (array == null) throw new NullPointerException();
197         if (array instanceof double[]) ((double[])array)[index] = z;
198         throw new IllegalArgumentException(jq_Reference.getTypeOf(array).toString());
199     }
200     private static Object newArray(Class componentType, int length)
201     throws NegativeArraySizeException {
202         jq_Type t = ClassLibInterface.DEFAULT.getJQType(componentType);
203         if (t == jq_Primitive.VOID)
204             throw new IllegalArgumentException("cannot create a void array");
205         jq_Array a = t.getArrayTypeForElementType();
206         a.cls_initialize();
207         return a.newInstance(length);
208     }
209     private static Object multiNewArray(Class componentType, int[] dimensions)
210     throws IllegalArgumentException, NegativeArraySizeException {
211         jq_Type a = ClassLibInterface.DEFAULT.getJQType(componentType);
212         if (a == jq_Primitive.VOID)
213             throw new IllegalArgumentException("cannot create a void array");
214         if (dimensions.length == 0)
215             throw new IllegalArgumentException("dimensions array is zero");
216         for (int i=0; i<dimensions.length; ++i) {
217             // check for dim < 0 here, because if a dim is zero, later dim's
218             // are not checked by multinewarray_helper.
219             if (dimensions[i] < 0)
220                 throw new NegativeArraySizeException("dim "+i+": "+dimensions[i]+" < 0");
221             a = a.getArrayTypeForElementType();
222             a.cls_initialize();
223         }
224         return joeq.Runtime.Arrays.multinewarray_helper(dimensions, 0, (jq_Array)a);
225     }
226     
227 }